const { ruleList, shareFriend, getUser } = require("../../../utils/api")

// pages/sharepage/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showRule: false,
    userInfo: {
      userimg: '',
      nickname: '',
      userid: '',
      point: ''
    },
    friendList: [
      {
        id: 0,
        head: '',
        name: '',
        isFriend: 0
      },
      {
        id: 1,
        head: '',
        name: '',
        isFriend: 0
      },
      {
        id: 2,
        head: '',
        name: '',
        isFriend: 0
      },
      {
        id: 3,
        head: '',
        name: '',
        isFriend: 0
      },
      {
        id: 4,
        head: '',
        name: '',
        isFriend: 0
      },
    ],
    ruleMsg: {},
    oneFriend: '',
    fiveFriend: ''
  },



  onBack() {
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getFriendList()
    this.getUserInfo()

  },

  toRule() {
    let that = this
    let params = {
      type: "4"
    }
    ruleList(params).then((res) => {
      if (res.errcode == 0) {
        that.setData({
          showRule: true,
          ruleMsg: res.data
        })

      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',

        });

      }
    })
  },

  async getUserInfo() {
    let that = this
    await getUser().then((res) => {

      if (res.errcode == 0) {
        that.setData({
          userInfo: res.user
        })

      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',

        });

      }
    })
  },

  async getFriendList() {
    let that = this
    await shareFriend().then((res) => {

      that.setData({
        oneFriend: res.invite1,
        fiveFriend: res.invite5,

      })

      var a = that.data.friendList
      var b = res.friendList;

      a.splice(0, b.length, ...b);
      var c = a.slice(b.length).concat(b.slice(a.length));
      a = a.slice(0, b.length).concat(c);

      that.setData({
        friendList: a
      })
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  onShareAppMessage(e) {
    const userInfo = JSON.parse(wx.getStorageSync('userInfo'));
    return {
      title: '我在收集积分，快来帮帮我',
      imageUrl: 'https://qmtestimage.oss-cn-guangzhou.aliyuncs.com/katong2.png',
      path: `/pages/homepage/home?shangjiuid=${userInfo.id}`,// 路径，传递参数到指定页面。

    }
  }

})